﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;

namespace NX5Web.ApiFilter
{
    public class ImgHelper
    {

        public static void imageZoom(string from, String to, Double zoomScale)
        {
            Bitmap btImage = new Bitmap(from);
            Bitmap serverImage = btImage;
            int width = (int)(serverImage.Width * zoomScale);
            int height = (int)(serverImage.Height * zoomScale);
            //画板大小
            int finalWidth = width, finalHeight = height;
            int srcImageWidth = serverImage.Width;
            int srcImageHeight = serverImage.Height;
            if (srcImageWidth > srcImageHeight)
            {
                finalHeight = srcImageHeight * width / srcImageWidth;
            }
            else
            {
                finalWidth = srcImageWidth * height / srcImageHeight;
            }


            //新建一个bmp图片
            Bitmap newImage = new Bitmap(width, height);
            //新建一个画板
            Graphics g = Graphics.FromImage(newImage);
            //设置高质量插值法
            g.InterpolationMode = InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(Color.White);
            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(serverImage, new Rectangle((width - finalWidth) / 2, (height - finalHeight) / 2, finalWidth, finalHeight), 0, 0, srcImageWidth, srcImageHeight, GraphicsUnit.Pixel);
            //以jpg格式保存缩略图
            MemoryStream msSaveImage = new MemoryStream();
            newImage.Save(to, ImageFormat.Jpeg);
            serverImage.Dispose();
            newImage.Dispose();
            g.Dispose();
        }

        public static void AddTextWatermark(string sourcePath, string outputPath, string watermarkText,
           string fontName = "微软雅黑", float fontSize = 12, Color color = default,
           WatermarkPosition position = WatermarkPosition.RightBottom, float opacity = 1f)
        {
            using (Image originalImage = Image.FromFile(sourcePath))
            using (Bitmap bitmap = new Bitmap(originalImage.Width, originalImage.Height))
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                // 清除背景色（透明）
                graphics.Clear(Color.Transparent);
                // 绘制原始图片
                graphics.DrawImage(originalImage, 0, 0, originalImage.Width, originalImage.Height);

                // 设置字体和画笔（带透明度）
                Font font = new Font(fontName, fontSize, FontStyle.Regular);
                Brush brush = new SolidBrush(Color.White);

                // 计算文字位置
                SizeF textSize = graphics.MeasureString(watermarkText, font);
                float x = 0, y = 0;
                switch (position)
                {
                    case WatermarkPosition.LeftTop:
                        x = 10;
                        y = 10;
                        break;
                    case WatermarkPosition.RightTop:
                        x = originalImage.Width - textSize.Width - 10;
                        y = 10;
                        break;
                    case WatermarkPosition.LeftBottom:
                        x = 10;
                        y = originalImage.Height - textSize.Height - 10;
                        break;
                    case WatermarkPosition.RightBottom:
                        x = originalImage.Width - textSize.Width - 10;
                        y = originalImage.Height - textSize.Height - 10;
                        break;
                    case WatermarkPosition.Center:
                        x = (originalImage.Width - textSize.Width) / 2;
                        y = (originalImage.Height - textSize.Height) / 2;
                        break;
                }
                graphics.FillRectangle(new SolidBrush(Color.FromArgb((int)(0.5 * 255), Color.Black)), 0, originalImage.Height - 200,
                    originalImage.Width, 200);

                // 绘制文字水印
                graphics.DrawString(watermarkText, font, brush, x, y);

                graphics.DrawString(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), font, brush,
                originalImage.Width - 460, y - 70);


                // 保存图片（保持原始格式）
                ImageFormat format = GetImageFormat(sourcePath);
                bitmap.Save(outputPath, format);
            }
        }

        /// <summary>
        /// 获取图片格式
        /// </summary>
        private static ImageFormat GetImageFormat(string filePath)
        {
            string extension = Path.GetExtension(filePath).ToLower();
            switch (extension)
            {
                case ".bmp": return ImageFormat.Bmp;
                case ".gif": return ImageFormat.Gif;
                case ".jpg": case ".jpeg": return ImageFormat.Jpeg;
                case ".png": return ImageFormat.Png;
                default: throw new NotSupportedException("不支持的图片格式");
            }
        }
    }

    public enum WatermarkPosition
    {
        LeftTop,
        RightTop,
        LeftBottom,
        RightBottom,
        Center
    }
}